<!DOCTYPE html>
<title>Test that the resource selection algorithm is restarted when load() is called, and that all "source" elements are reconsidered.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="media-file.js"></script>
<video></video>
<script>
async_test(function(t) {
    var sourceUrls = [];
    var errorCount = 0;
    var video = document.querySelector("video");

    // Test initial networkState.
    assert_equals(video.networkState, HTMLMediaElement.NETWORK_EMPTY);

    video.oncanplaythrough = t.step_func(function() {
        var url = video.currentSrc;
        assert_equals(url.substr(url.lastIndexOf("/media/") + 7),
            sourceUrls[1].substr(sourceUrls[1].lastIndexOf("/media/") + 7));
        switch (errorCount) {
        case 1:
            // Calling load() to invoke resource selection again.
            video.load();
            break;
        case 2:
            t.done();
        }
    });

    video.addEventListener("error", function() {
        errorCount++;
    }, true);

    // Add an invalid url to the first source so we test getting
    // an error event each time resource selection runs.
    addSource("content/bogus.ogv");
    addSource("content/test.ogv");
    addSource("content/test.oga");

    // test networkState.
    assert_equals(video.networkState, HTMLMediaElement.NETWORK_NO_SOURCE);

    function addSource(filePath) {
        var source = document.createElement("source");
        source.src = filePath;
        sourceUrls.push(source.src);
        source.type = mimeTypeForExtension(source.src.split(".").pop());
        video.appendChild(source);
    }
});
</script>